﻿using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

	public int speed = 1;
	public Camera selectedCamera;
	public float minPinchSpeed = 2.0f;
	public float varianceInDistances = 5.0f;
	private float touchDelta = 0.0f;
	private Vector2 prevDist = new Vector2(0,0);
	private Vector2 curDist = new Vector2(0,0);
	private float speedTouch0 = 0.0f;
	private float speedTouch1 = 0.0f;

	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (Input.touchCount == 2 && 
		    Input.GetTouch(0).phase == TouchPhase.Moved && 
		    Input.GetTouch(1).phase == TouchPhase.Moved) 
		{
			
			curDist 	= 	Input.GetTouch(0).position - Input.GetTouch(1).position; 
			prevDist 	= (	(Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - 
			             	(Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition));
			touchDelta 	= 	curDist.magnitude - prevDist.magnitude;
			speedTouch0 = 	Input.GetTouch(0).deltaPosition.magnitude / Input.GetTouch(0).deltaTime;
			speedTouch1 = 	Input.GetTouch(1).deltaPosition.magnitude / Input.GetTouch(1).deltaTime;
		
			if ((touchDelta + varianceInDistances <= 1) && 
			    (speedTouch0 > minPinchSpeed) 			&& 
			    (speedTouch1 > minPinchSpeed))
			{
				selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView + (1 * speed),15,150);
			}
			
			if ((touchDelta + varianceInDistances > 1) 	&& 
			    (speedTouch0 > minPinchSpeed) 			&& 
			    (speedTouch1 > minPinchSpeed))
			{		
				selectedCamera.fieldOfView = Mathf.Clamp(selectedCamera.fieldOfView - (1 * speed),15,90);
			}
			
		}     
	}
}
